import axios from '../utils/request'
import { reject } from 'q'

const getCurrentCity = () => {
  return new Promise(resolve => {
    let currentCity = JSON.parse(localStorage.getItem('hzk_city')) || ''
    // console.log(currentCity)
    if (currentCity) {
      // console.log(222)
      return resolve(currentCity)
    }
    const myCity = new window.BMap.LocalCity()
    myCity.get(async res => {
      try {
        // currentCity = name.slice(0, name.length - 1)
        // resolve(currentCity)
        const { body } = await axios.get(`/area/info?name=${res.name}`)
        // console.log(body)
        // localStorage.setItem('localCity')
        currentCity = body
        // console.log(currentCity)
        localStorage.setItem('hzk_city', JSON.stringify(currentCity))
        resolve(currentCity)
      } catch (err) {
        reject(err)
      }
    })
    // console.log(currentCity)
  })
}

export { getCurrentCity }
